package com.bdqn.t320.bootsecurity.controller;


import com.bdqn.t320.bootsecurity.entity.User;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    /**
     * 默认首页
     *
     * @return
     */
    @RequestMapping("/")
    public String index() {
        return "login";
    }

    /**
     * 登录页面
     *
     * @return
     */
    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    /**
     * 页面登录controller
     *
     * @param session
     * @return
     */
    @RequestMapping("/dologin")
    public String dologin(HttpSession session) {
        //SecurityContext context_session = (SecurityContext) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
        //System.out.println(context_session.getAuthentication().getName());
        SecurityContext context = SecurityContextHolder.getContext();
        //context == context_session  true
        User user = new User();
        user.setUsrName(context.getAuthentication().getName());
        session.setAttribute("user", user);
        return "main";
    }

    /**
     * 授权测试页面
     *
     * @return
     */
    @RequestMapping("/user")
    public String user() {
        return "user";
    }

    /**
     * 授权测试页面
     *
     * @return
     */
    @RequestMapping("/admin")
    public String admin() {
        return "admin";
    }

    @RequestMapping("/login_view")
    public String login_view() {
        return "login";
    }

    @RequestMapping("/timeout")
    public String timeout() {
        return "timeout";
    }

    @RequestMapping("/logout")
    public String logout() {
        return "login";
    }

}
